<?php
class Tools_Date 
{
    // 周一是本周开始的第一天
    protected static $_weekly_first_day = '1';
    protected static $_monthly_first_day = '24';

    public static function between_months ($start_day, $stop_day = NULL)
    {
        $t = strtotime($start_day);
        $stop_day = empty($stop_day) ? time() : $stop_day;
        $ms = array();
        while ($t <= $stop_day)
        {
            $m = date("Ym", $t);
            if (! isset($ms[$m]))
                $ms[$m] = 1;

            $t += 86400;
        }

        return array_keys($ms);
    }

    public static function weekly_start_stop_days ($today = NULL)
    {
        if (empty($today))
            $today = date("Y-m-d");

        $t_today = strtotime($today);
        $i_today = getdate($t_today);

        $wday = $i_today['wday'];
        if ($wday == 0) 
            $wday = 7;
        $t_first = (self::$_weekly_first_day - $wday) * 86400 + $t_today;
        $t_last  = $t_first + 86400 * 6;

        return array(date("Y-m-d", $t_first), date("Y-m-d", $t_last));
    }

    public static function monthly_start_stop_days ($today = NULL)
    {
        if (empty($today))
            $today = date("Y-m-d");

        $t_today = strtotime($today);
        $i_today = getdate($t_today);

        $mday     = $i_today['mday'];
        $mon      = $i_today['mon'];
        $year     = $i_today['year'];

        $next_mon = $prev_mon = $mon;
        $next_year= $prev_year= $year;

        $kday = self::$_monthly_first_day;
        if ($mday > $kday)
        {
            $next_mon += 1;
            $next_year = $year;
            $prev_mon  = $mon;
            $prev_year = $year;
        }
        else
        {
            $next_mon  = $mon;
            $next_year = $year;
            $prev_mon  -= 1;
            $prev_year = $year;

        }

        return array(
            date("Y-m-d", mktime(0, 0, 0, $prev_mon, $kday, $prev_year)),
            date("Y-m-d", mktime(0, 0, 0, $next_mon, $kday, $next_year))
            );
    }
}

